<?php
namespace Home\Controller;
use Think\Controller;

class OrderoverController extends Controller{
	public function index(){
		$order_number=I('get.order_number');
		$order=M('orders');

		//根据订单号查询订单表里面的信息
		$orders=$order->where('order_number='.$order_number)->find();
		$this->assign('orders',$orders);
		$this -> display();
	}

	public function add(){

		$order=M('orders');
		$data=I('post.');
		$data['user_id']=$_SESSION['id'];

		$order->create();
		$dd=$order->data($data)->add();

		//获取订单号
		$order_number=I('post.order_number');

		$order=M('orders');
		//根据订单号查询订单表里面的信息
		$orders=$order->where('order_number='.$order_number)->find();

		//查询出提交订单后所包含的商品信息
		$ids=$orders['ids'];
		$userid=$orders['user_id'];
		$cart=M('Cart');
		$map['goodsid']=array('in',$ids);
		$map['userid']=$userid;
		$mycart=$cart->where($map)->select();

		//把查询出来的信息插入订单详情表中
		//实例化详情表
		$detail=M('Detail');
		$goods = M('goods');
		$detail->create();
		foreach ($mycart as $k => $v) {
			$info['user_id']=$v['userid'];
			$info['goods_id']=$v['goodsid'];
			$info['goods_name']=$v['goodsname'];
			$info['price']=$v['goodsprice'];
			$info['num']=$v['number'];
			$info['xiaoji']=$v['xiaoji'];
			$info['goodspic']=$v['goodspic'];
			$info['order_number']=$order_number;

			$detail->data($info)->add();
			$store['store'] = array('exp','store-'.$v['number']);
			$goods->where('goodsid='.$v['goodsid'])->data($store)->save();
		}

		$cart->where($map)->delete();


		//将传入的值赋给留言表
		$arr['user_id']=$_SESSION['id'];
		$arr['content']=$data['speak'];
		$arr['order_number']=$data['order_number'];
		$arr['addtime']=$data['addtime'];
		$arr['username']=$_SESSION['username'];

		//实例化留言表
		$message=M('message');
		 $message->create();
		 $message->add($arr);

		if($dd){
			$this->success('下单成功',''.__MODULE__.'/Orderover/index/order_number/'.$order_number.'');
		}else{
			$this->error('下单失败',''.__MODULE__.'/Orderover/index/order_number/'.$order_number.'');
		}
	}

	//支付后改变订单状态
	public function pay(){


		$order_number = I('get.order_number');
		$order=M('orders');
		$order->create();

		$orders=$order->where('order_number='.$order_number)->data("state=已支付")->save();
		if($orders){
			$this->success('订单支付成功',''.__MODULE__.'/Order/myorder/order_number/'.$order_number.'');
		}else{
			$this->error('订单支付失败',''.__MODULE__.'/Order/myorder/order_number/'.$order_number.'');
		}
	}
	
}